import 'package:coinbaby/common/const/cosnt.dart';
import 'package:coinbaby/common/textstyle/textstyle.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class PwdLoginPage extends StatefulWidget {
  @override
  _PwdLoginPageState createState() => _PwdLoginPageState();
}

class _PwdLoginPageState extends State<PwdLoginPage> {

  final FocusNode _phoneFocusNode = FocusNode();
  final FocusNode _pwdFocusNode = FocusNode();
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _pwdController = TextEditingController();

  void _login() {
    _jumpToRoot();
  }

  void _jumpToRoot(){
    SharedPreferences.getInstance().then((value) => value.setBool(AppConst.APP_IS_LOGIN, true));
    Navigator.of(context).pushNamedAndRemoveUntil('/root',(Route<dynamic> route) => false);
  }

  void _unfocus() {
    _phoneFocusNode.unfocus();
    _pwdFocusNode.unfocus();
  }

  @override
  void dispose() {
    _phoneController.dispose();
    _pwdController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    
    final size = MediaQuery.of(context).size;
    final marginH = size.height * 0.06;

    return Scaffold(
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: _unfocus,
        child: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.keyboard_arrow_left),
                onPressed: () => Navigator.pop(context),
              ),
              SizedBox(height: marginH),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 20),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text("账号密码登录", style: AppFont.textStyle(26, Colors.black, fontWeight: FontWeight.bold),),
                    SizedBox(height: marginH),
                    TextField(
                      maxLength: 11,
                      keyboardType: TextInputType.number,
                      controller: _phoneController,
                      focusNode: _phoneFocusNode,
                      decoration: InputDecoration(
                        hintText: "请输入手机号码",
                        counterText: "",
                        hintStyle: AppFont.textStyle(14, Colors.grey[300]),
                        labelStyle: AppFont.textStyle(14, Colors.black)
                      ),
                    ),
                    SizedBox(height: 18,),
                    TextField(
                      obscureText: true,
                      focusNode: _pwdFocusNode,
                      controller: _pwdController,
                      decoration: InputDecoration(
                        hintText: "请输入密码",
                        hintStyle: AppFont.textStyle(14, Colors.grey[300]),
                        labelStyle: AppFont.textStyle(14, Colors.black),
                      ),
                    ),

                    InkWell(
                      onTap: _login,
                      child: Container(
                        width: size.width,
                        height: 50,
                        margin: EdgeInsets.only(top: marginH),
                        decoration: BoxDecoration(
                          color: Colors.red,
                          borderRadius: BorderRadius.circular(10)
                        ),
                        alignment: Alignment.center,
                        child: Text("登陆", style: AppFont.textStyle(14, Colors.white),),
                      ),
                    ),

                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        InkWell(
                          onTap: () => Navigator.pop(context),
                          child: Padding(
                            padding: const EdgeInsets.only(top: 18.0),
                            child: Text('验证码登录', style: AppFont.textStyle(14, Colors.blue),),
                          ),
                        ),
                        InkWell(
                          onTap: () => Navigator.pushNamed(context, "/forgetpwd"),
                          child: Padding(
                            padding: const EdgeInsets.only(top: 18.0),
                            child: Text("忘记密码?", style: AppFont.textStyle(14, Colors.grey),),
                          ),
                        ),
                      ],
                    )
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}